package com.test.executorPool;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 * 如果 maxSize >  运行的线程 > corePoolSize 情况下。
 * 等待队列是asynchronous, 那么keepAliveTime 起作用了 。
 * 删除等待 时间 超长的线程
 *
 *
 * @author:wesley
 */
public class ThreadPoolExecutorTest3 {


    public static void main(String[] args) throws InterruptedException {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+"--"+System.currentTimeMillis());
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };


        ThreadPoolExecutor executor = new ThreadPoolExecutor(7, 8 ,
                5, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

        executor.execute(runnable);
        executor.execute(runnable);
        executor.execute(runnable);
        executor.execute(runnable);
        executor.execute(runnable);
        executor.execute(runnable);
        executor.execute(runnable);

        executor.execute(runnable);


        // 此时打印获取队列中的个数
        Thread.sleep(300);
        System.out.println("A==="+executor.getCorePoolSize());
        System.out.println("A==="+executor.getPoolSize());
        System.out.println("A==="+executor.getQueue().size());


        // 等待时间过久，删除超时等待的线程
        Thread.sleep(10000);
        System.out.println("b==="+executor.getCorePoolSize());
        System.out.println("b==="+executor.getPoolSize());
        System.out.println("b==="+executor.getQueue().size());

    }



}
